package Fly.example.service.impl;

import Fly.example.dto.SetmealDto;
import Fly.example.pojo.Setmeal;
import Fly.example.mapper.SetmealMapper;
import Fly.example.pojo.SetmealDish;
import Fly.example.service.ISetmealDishService;
import Fly.example.service.ISetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author 陽Fly
 * @since 2025-06-14
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {
    @Autowired
    private ISetmealDishService setmealDishService;
    /*
    * 新增套餐，同时写入套餐关系表
    * */
    @Override
    @Transactional//事务操作，两张表的一致性
    public void savaWithStemeal(SetmealDto setmealDto) {
        //保存菜品信息
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item->{
            item.setSetmealId(String.valueOf(setmealDto.getId()));
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}
